package com.prd.controller;


import com.prd.enums.BizCodeEnum;
import com.prd.model.UserDO;
import com.prd.request.UserLoginRequest;
import com.prd.request.UserRegisterRequest;
import com.prd.service.FileService;
import com.prd.service.UserService;
import com.prd.util.JsonData;
import com.prd.util.Remote;
import com.prd.vo.LoginRegisterVo;
import com.prd.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhaoyi
 * @since 2023-10-23
 */
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {

    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;

    /**
     * 上传用户头像
     *
     * 默认最大是1M,超过则报错
     *
     * @param file 文件
     * @return
     */
    @ApiOperation("图片上传")
    @PostMapping(value = "upload")
    public JsonData uploadUserImg(
            @ApiParam(value = "文件上传",required = true)
            @RequestPart("file") MultipartFile file){

        String result = fileService.uploadUserImg(file);

        return result!=null? JsonData.buildSuccess(result):JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }

    @ApiOperation("设置密码")
    @PostMapping("setPassword")
    public JsonData setPassword(@ApiParam("密码") @RequestParam(value = "pwd") String pwd){

        return userService.setPassword(pwd);
    }

    @ApiOperation("用户注册登录")
    @PostMapping("registerAndLogin")
    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    public JsonData registerAndLogin(@RequestBody LoginRegisterVo loginRegisterVo){

        return userService.registerAndLogin(loginRegisterVo);
    }

    @ApiOperation("更新用户信息")
    @PostMapping("updateUserInfo")
    public JsonData updateUserInfo(@RequestBody UserVO userVO){

        return userService.updateUserInfo(userVO);
    }

    /**
     * 用户个人信息查询
     * @return
     */
    @ApiOperation("个人信息查询")
    @GetMapping("detail")
    public JsonData detail(){

        return userService.findUserDetail();
    }

    /**
     * 用户个人信息查询
     * @param ids
     * @return
     */
    @ApiOperation("个人信息查询")
    @GetMapping("listInfo/{ids}")
    public Remote<List<UserDO>> listInfo(@PathVariable Long[] ids){
        return Remote.ok(userService.listByIds(Arrays.asList(ids)));
    }


}

